package com.hnichr.ump.onetime.sortseat.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.hnichr.ump.common.onetime.sortseat.entity.SeatMan;
import com.hnichr.ump.common.onetime.sortseat.service.SeatManService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SeatManServiceImpl extends BaseServiceImpl<SeatMan> implements SeatManService {

    @Override
    @Transactional(readOnly = true)
    public List<Map<String,String>> sortSeat(){
        List<Map<String,String>> resultList = new ArrayList<>();
        List<SeatMan> allSeatManList = JpaUtil.findAll(SeatMan.class);
        List<String> seatManList = allSeatManList.stream().map(SeatMan::getName).collect(Collectors.toList());
        int deskNum = 1;
        int pos;
        while(!seatManList.isEmpty()){
            //随机第一个
            pos = RandomUtil.randomInt(0, seatManList.size());
            Map<String,String> mp = new HashMap<>();
            mp.put(String.valueOf(deskNum),seatManList.get(pos));
            seatManList.remove(pos);
            resultList.add(mp);
            deskNum++;
        }
        return resultList;
    }
}
